﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.BaseModule.Optimization
{
    public class SingleDataSourceOptimizationData : SgingOPtimizationBase
    {
        public SingleDataSourceOptimizationData(DataContext context)
        {
            dataContext = context;
        }

        /// <summary>
        /// 执行优选
        /// </summary>
        /// <param name="dataModelAsis">分析数据源</param>
        /// <param name="dataModelOP">优选数据源</param>
        /// <param name="opsum">优选数据集</param>
        /// <param name="assum">分析数据集</param>
        /// <param name="xmllableConfig">配置</param>
        /// <param name="soursnameopid">数据源id</param>
        /// <param name="soursnameasisid">数据源id</param>
        public void Optimization(DataModel dataModelAsis, DataModel dataModelOP, int opsum, int assum, Xmllable xmllableConfig, string soursnameopid, string soursnameasisid)
        {
            SgingBase(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
        }
        /// <summary>
        /// 执行优选
        /// </summary>
        /// <param name="dataModelAsis">分析数据源</param>
        /// <param name="dataModelOP">优选数据源</param>
        /// <param name="opsum">优选数据集</param>
        /// <param name="assum">分析数据集</param>
        /// <param name="xmllableConfig">配置</param>
        /// <param name="soursnameopid">数据源id</param>
        /// <param name="soursnameasisid">数据源id</param>
        public void Optimization1(DataModel dataModelAsis, DataModel dataModelOP, int opsum, int assum, Xmllable xmllableConfig, string soursnameopid, string soursnameasisid)
        {
            string sourCode = dataModelAsis.sourceData.sourceList[0].SourceCode;

            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceNodeOP = dataModelOP.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == sourCode.ToLower());
            if (sourceNodeOP != null)
            {
                sourceNodeOP = dataModelAsis.sourceData.sourceList[0];
            }
            else
            {
                Source sourceopdata = dataModelAsis.sourceData.sourceList[0];
                sourceopdata.IsCheck = "0";
                dataModelOP.sourceData.sourceList.Add(sourceopdata);
            }
            if (assum <= opsum)
            {
                foreach (Source sourceNode in dataModelOP.sourceData.sourceList)
                {

                    sourceNode.AnalysisStatus = "1";

                    if (sourceNode.SourceCode.ToLower() == sourCode.ToLower())
                    {
                        sourceNode.IsCheck = "1";
                    }
                    else
                    {
                        sourceNode.IsCheck = "0";
                    }
                }
            }

            //循环比较属性信息
            foreach (AttributeInfo attritem in dataModelAsis.AttributesList)
            {
                AttributeInfo attributeop = dataModelOP.AttributesList.FirstOrDefault(a => a.Name == attritem.Name && a.Name != "id");
                if (attributeop == null)
                {
                    AttributeInfo attributeTmp = new AttributeInfo();
                    attributeTmp.Name = attritem.Name;
                    attributeTmp.DictionariesCode = attritem.DictionariesCode;
                    attributeTmp.Priority = attritem.Priority;
                    attributeTmp.sourceList = attritem.sourceList;
                    dataModelOP.AttributesList.Add(attributeTmp);
                }
                else
                {
                    if (attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) == null)
                    {
                        Source attsoucremodel = attritem.sourceList[0];
                        attsoucremodel.IsCheck = "0";
                        attributeop.sourceList.Add(attsoucremodel);
                    }

                    if (xmllableConfig.ElementList != null)
                    {

                        LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Element element = xmllableConfig.ElementList.FirstOrDefault(a => a.Name == attritem.Name);
                        if (element != null && !string.IsNullOrEmpty(element.Priority))
                        {
                            opsum = ListByCount(element.Priority.Split('>').ToList(), soursnameopid);
                            assum = ListByCount(element.Priority.Split('>').ToList(), soursnameasisid);
                        }
                    }

                    if (assum < opsum)
                    {
                        foreach (LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source attsource in attributeop.sourceList)
                        {
                            attsource.AnalysisStatus = "1";
                            if (attsource.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                            {
                                attsource.IsCheck = "1";
                            }
                            else
                            {
                                attsource.IsCheck = "0";
                            }
                        }
                    }
                    else if (assum == opsum)
                    {
                        try
                        {


                            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceasis = attritem.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceop = attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                            attributeop.sourceList.Remove(sourceop);
                            attributeop.sourceList.Add(sourceasis);
                        }
                        catch (Exception ef)
                        {

                           
                        }
                    }

                    if (assum <= opsum)
                    {
                        foreach (Source sourceNode in attributeop.sourceList)
                        {
                            sourceNode.AnalysisStatus = "1";
                            if (sourceNode.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                            {
                                sourceNode.IsCheck = "1";
                            }
                            else
                            {
                                sourceNode.IsCheck = "0";
                            }
                        }
                    }
                }
            }
        }
    }
}
